import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author fenge
 */
public class DeadLock {

    public static void main(String[] args) {
        MySource mySource1 = new MySource();
        MySource mySource2 = new MySource();

        Lock reentrantLock1 = new ReentrantLock();
        Lock reentrantLock2 = new ReentrantLock();

        new Thread(() -> {
            mySource1.lock.lock();
            System.out.println(Thread.currentThread().getName() + "\t" + "自己持有A锁，希望获得B锁");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            {
                mySource2.lock.lock();
                System.out.println(Thread.currentThread().getName() + "\t" + "获得B锁成功");
            }
        }).start();
        new Thread(() -> {
            mySource2.lock.lock();
            System.out.println(Thread.currentThread().getName() + "\t" + "自己持有B锁，希望获得A锁");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            {
                mySource1.lock.lock();
                System.out.println(Thread.currentThread().getName() + "\t" + "获得B锁成功");
            }

        }).start();

    }
}

class MySource {
    public Lock lock = new ReentrantLock();
}
